package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectAnswerDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.convert.SubjectInfoConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfoEs;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题controller
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 新增题目
     *
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }

            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult()
                    , "题目难度不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds())
                    , "标签id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds())
                    , "分类id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDTOConverter.INSTANCE
                    .convertDTOtoBo(subjectInfoDTO);
            List<SubjectAnswerBo> subjectAnswerBos = SubjectAnswerDTOConverter.INSTANCE
                    .convertListDTOtoBo(subjectInfoDTO.getOptionList());
            subjectInfoBo.setOptionList(subjectAnswerBos);

            subjectInfoDomainService.add(subjectInfoBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }

    }

    /**
     * 查询题目列表
     *
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId()
                    , "题目分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "题目标签id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDTOConverter.INSTANCE
                    .convertDTOtoBo(subjectInfoDTO);

            PageResult<SubjectInfoBo> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.fail("查询题目列表失败");
        }

    }

    /**
     * 查询题目详情
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getId()
                    , "题目id不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDTOConverter.INSTANCE
                    .convertDTOtoBo(subjectInfoDTO);
           SubjectInfoBo boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBo);
            SubjectInfoDTO dto = SubjectInfoDTOConverter.INSTANCE.convertBotoDTO(boResult);

            return Result.ok(dto);
        } catch (Exception e) {
            log.error("SubjectCategoryController.querySubjectInfo .error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }

    }

    /**
     * 全文检索
     *
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            //参数校验
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWord()),
                    "关键词不能为空");


            SubjectInfoBo subjectInfoBo = SubjectInfoDTOConverter.INSTANCE
                    .convertDTOtoBo(subjectInfoDTO);
               subjectInfoBo.setPageNo(subjectInfoDTO.getPageNo());
               subjectInfoBo.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoEs> boPageResult = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBo);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }

    }
/**
 *获取题目贡献榜
 */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {

        try {
            List<SubjectInfoBo> boList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> dtoList = SubjectInfoDTOConverter.INSTANCE.convertBOToDTOList(boList);
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取贡献榜失败");
        }
    }

/**
 *测试mq方法
 */
    @PostMapping("/pushMessage")
    public Result<Boolean> pushMessage(@Param("id")int id) {
        rocketMQTemplate.convertAndSend("first-topic","hello,mq"+id);
    return Result.ok(true);
    }
}
